<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
    <!-- load css -->
    <link rel="stylesheet" type="text/css" th:href="@{/plugin/layui/css/font/iconfont.css}" media="all"/>
    <link rel="stylesheet" type="text/css" th:href="@{/plugin/layui/js/layui/css/layui.css}"  media="all"/>
    <link rel="stylesheet" type="text/css" th:href="@{/plugin/bootstrap/bootstrap/css/bootstrap.min.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/plugin/bootstrap-table/bootstrap-table.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/table.css}"  media="all"/>
    <link rel="stylesheet" type="text/css" th:href="@{/plugin/bootstrap/bootstrap/css/bootstrap.min.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/plugin/layui/js/select2/css/select2.css}" media="all"/>

    <style>
        h2{
            margin: 0;
        }
        .blocks{
            display: flex;
            flex-direction: column;
            width: 20rem;
            height: 6rem;
        }
        .blockout{
            display: flex;
            justify-content: space-between;
            margin: 1.5rem 0;
            padding: 0 1rem;
        }
        #auditType{
            display: inline-block;
            min-width: 6rem;
            height: 3rem;
            background-color: #409EFF;
            border-radius: 4px;
            color: white;
            text-align: center;
            line-height: 3rem;
        }
        input[type="text"]:disabled{
            background-color: #EEF1F6;
        }
        input[type="text"],select{
            height: 3rem;
        }
        #suppplierDetail{
            display: none;
        }
        #tableForm{
            display: none;
        }
        #addInfor{
            width: 10rem;
            margin-left: 1rem;
            position: relative;
            top: 1rem;
            z-index: 1;
        }
        #TreeDiv{
            display: flex;
            justify-content: space-between;
        }
        #tree{
            width: 50%;
            height: 60vh;
            overflow: auto;
        }
        #nodesDetail{
            width: 40%;
            height: 15rem;
            border: 1px solid #dddddd;
            border-radius: 0.4rem;
            box-shadow:0 1px 6px rgba(176,196,222,1);

        }
        #nodesDetail div{
            height: 7rem;
            font-size: 1.7rem;
            line-height: 7rem;
            text-indent: 3rem;
        }
    </style>
</head>
<body>
<div class="layui-fluid larry-wrapper" id="StandardVUE">
    <div class="layui-row layui-col-space30">
        <div class="layui-col-xs24">
            <section class="panel panel-padding">
                <div class="layui-collapse" lay-filter="test">
                    <form id="form1" class="layui-form layui-form-pane" action="">
                        <div id="orderDetail">
                            <div id="tableForm">
                                <input type="hidden" name="id"/>
                                <div class="blockout">
                                    <div class="blocks">
                                        <label>货品编号</label>
                                        <div class="layui-input-inline" id="goodsCodeS">
                                            <!--<select lay-filter="goodsCode"-->
                                                    <!--lay-search=""-->
                                                    <!--id="goodsCode"-->
                                                    <!--name="goodsCode"-->
                                                    <!--placeholder="货品编号">-->
                                            <!--</select>-->
                                            <select class="form-control selectpicker bla bla bli "
                                                    lay-ignore="lay-ignore"
                                                    id="goodsCode"
                                                    name="goodsCode"
                                                    select2
                                                    placeholder="货品编号">
                                            </select>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>货品名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled class="layui-input" name="goodsName" id="goodsName" placeholder="货品名称"/>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>包装名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled class="layui-input" name="packName" id="packName" placeholder="包装名称"/>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>入库单位</label>
                                        <div class="layui-input-inline">
                                            <select class="form-control selectpicker bla bla bli "
                                                    lay-filter="warehouse"
                                                    id="warehouse"
                                                    name="warehouse"
                                                    placeholder="入库单位">
                                            </select>
                                            <span hidden id="warehouseSpan"></span>
                                        </div>
                                    </div>
                                </div>
                                <div class="blockout">
                                    <div class="blocks">
                                        <label>体积</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled class="layui-input" name="volume" id="volume" placeholder="体积"/>
                                            <span hidden id="volumeS"></span>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>净重</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled class="layui-input" name="suttle" id="suttle" placeholder="净重"/>
                                            <span hidden id="suttleS"></span>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>毛重</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled class="layui-input" name="grossWeight" id="grossWeight" placeholder="毛重"/>
                                            <span hidden id="grossWeightS"></span>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>金额</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled class="layui-input" name="money" id="money" placeholder="金额"/>
                                        </div>
                                    </div>
                                </div>
                                <div class="blockout">
                                    <div class="blocks">
                                        <label>主单位</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled="true" class="layui-input" name="unitNumber" id="unitNumber" placeholder="主单位"/>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>箱单位</label>
                                        <div class="layui-input-inline">
                                            <input type="text" disabled="true" class="layui-input" name="boxNumber" id="boxNumber" placeholder="箱单位"/>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>总金额</label>
                                        <div class="layui-input-inline">
                                            <input type="text" class="layui-input" name="numMoney" id="numMoney" />
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>指定库位</label>
                                        <div class="layui-input-inline">
                                            <select lay-filter="appointPlace"
                                                    lay-search=""
                                                    id="appointPlace"
                                                    name="appointPlace"
                                                    placeholder="货品编号">
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="blockout" style="width: 50%">
                                    <div class="blocks">
                                        <label>指定批次</label>
                                        <div class="layui-input-inline">
                                            <select lay-filter="appointBatch"
                                                    lay-search=""
                                                    id="appointBatch"
                                                    name="appointBatch"
                                                    placeholder="指定批次">
                                            </select>
                                        </div>
                                    </div>
                                    <div class="blocks">
                                        <label>指定生产日期</label>
                                        <div class="layui-input-inline">
                                            <select lay-filter="appointDate"
                                                    lay-search=""
                                                    id="appointDate"
                                                    name="appointDate"
                                                    placeholder="指定生产日期">
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="blockout">
                                    <div class="blocks" style="width: 69%">
                                        <label>备注</label>
                                        <div class="layui-input-inline">
                                            <input type="text" class="layui-input" name="datailRemark" id="datailRemark"/>
                                        </div>
                                    </div>
                                    <span hidden id="INDEXS"></span>
                                    <div class="blocks">
                                        <div class="layui-input-inline" style="margin-top: 2rem;">
                                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="add" style="text-decoration: none;">保存</button>
                                            <a class="layui-btn layui-btn-primary" onclick="tableFormReset()" style="text-decoration: none;">取消</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <a class="layui-btn layui-btn-sm layui-btn-normal" id="addInfor" onclick="tableFormShow()" style="text-decoration: none;">新增明细</a>
                            <section class="panel panel-padding layui-card-body" >
                                <div class="pro">
                                    <table id="table" data-undefined-text=">_<" data-height="400" class="table text-nowrap"></table>
                                </div>
                            </section>
                        </div>
                    </form>
                </div>
            </section>
        </div>
    </div>
</div>
</body>
<div th:include="public/public_js::public_js"></div>
<script th:src="@{plugin/beyond/assets/js/fuelux/wizard/wizard-custom.js}"></script>

<script>

    var indexs = 0;
    var supplierInf;
    var POtypeData;



    // option渲染方法
    function optionRendering(data, type, value, text1, text2) {
        var optionInf = '';
        if (type === 1) {
            data.forEach(function (item) {
                optionInf += '<option value="'+ item[value] +'">'+ item[text1] +'</option>';
            })
        }
        if (type === 2) {
            data.forEach(function (item) {
                optionInf += '<option value="'+ item[value] +'">'+ item[text1] + ' ' + item[text2] +'</option>';
            })
        }

        return optionInf;
    }

    // 根据包装类型判断主单位是否禁用
    function packageType(type) {
        if (type == 0) {
            $('#unitNumber').attr("disabled",false);
            $('#boxNumber').attr("disabled",true);
        } else {
            $('#unitNumber').attr("disabled",true);
            $('#boxNumber').attr("disabled",false);
        }
    }

    function returnpage() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }

    function toFixeds (data) {
        var str = "";
        if (data) {
            data = data.toString();
            if (data.indexOf('.') < 0) {
                str = data;
            } else {
                str = data.substring(0, data.indexOf('.') + 3);
            }
            return parseFloat(str);
        } else {
            return data;
        }
    }

    layui.use(['myform','zzpUtils','form','laydate','jquery','zzpValidation','element'],function () {
        var form = layui.form,
            zzpUtils = layui.zzpUtils
            ,laydate = layui.laydate
            ,zzpValidation = layui.zzpValidation
            ,$ = layui.jquery;
        var element = layui.element;
        element.init();


        var ID = localStorage['id'], UnitNum, UnitType, INDEXS = 0, INDEXS = 0, addType = 'add';

        function querytable() {
            var Data;
            $.ajax({
                url: "/zxsdwms/wmsshipmentorder/getDetailList",
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                async: false,
                data:JSON.stringify({id:ID}),
                success: function (data) {
                    console.log('datadata', data);
                    Data = data;

                }
            })
            var TableData = [];
            Data.forEach(function (item) {
                var newitem = {};
                newitem = item;
                newitem.goodsbh = item.bh;
                newitem.goodsName = item.mc;
                newitem.packName = item.bzmc;
                newitem.warehouse = item.ms;
                newitem.unitNumber = item.yqsl;
                newitem.boxNumber = item.yqxdwsl;
                TableData.push(newitem);
            });
            return TableData;
        }

        //查询货品编号
        function goodsCode(ids) {
            var DATA;
            $.ajax({
                url: '/zxsdwms/wmsshipmentorder/getGoodsByHzid',
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(ids),
                async: false,
            }).then(function (resolve) {
                DATA = resolve;
            })
            return DATA;
        }

        // 查询入库单位
        function wareHouseUnit(id) {
            var DATA;
            $.ajax({
                url: '/zxsdwms/wmsPurchaseOrder/packDetailList/' + id,
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                async: false,
                success: function (data) {
                    DATA = data;
                }
            })
            return DATA;
        }

        // 查询指定库位
        function QueryAppointPlace(data) {
            var DATA;
            $.ajax({
                url: '/zxsdwms/wmsshipmentorder/getLiLibraryPosition',
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(data),
                async: false,
                success: function (data) {
                    DATA = data;
                }
            })
            return DATA;
        }

        // 查询指定批次
        function QueryAppointBatch(data) {
            var DATA;
            zzpUtils.action({
                url:"/wmsshipmentorder/getWmsStock",
                data: JSON.stringify(data),
                async: false,
                success:function (data) {
                    DATA = data;
                },
                error: function (xhr, textStatus) {
                    console.log(textStatus);
                    console.log(xhr)
                }
            });
            return DATA;
        }

        // 查询指定生产日期
        function QueryAppointDate(data) {
            var DATA;
            zzpUtils.action({
                url:"/wmsshipmentorder/getWmsStockScrq",
                data: JSON.stringify(data),
                async: false,
                success:function (data) {
                    DATA = data;
                },
                error: function (xhr, textStatus) {
                    console.log(textStatus);
                    console.log(xhr)
                }
            });
            return DATA;
        }

//        form.on('select(goodsCode)', function (data) {
//            console.log('货品编号选中触发');
//            if (data !== '') {
//                var text = $("#goodsCode").find("option:selected").text();
//                var ids = {'bh': text};
//                var goodsCodeData = goodsCode(ids)[0];
//                $('#goodsName').val(goodsCodeData.bzmc);//包装名称
//                $('#packName').val(goodsCodeData.bzmc);//货品名称
//                var packId = goodsCodeData.packId;
//                var warehouseunit = wareHouseUnit(packId);
//                warehouseOption(warehouseunit);
//                formSelectApply(data.value);
//            }
//        })

        // 货品编号的选中触发
        $("#goodsCode").on("select2:select",function(e){
            var data = e.params.data;
            if (data !== '') {
//                var text = $("#goodsCode").find("option:selected").text();
                var ids = {'bh': data.text};
                var goodsCodeData = goodsCode(ids)[0];
                $('#goodsName').val(goodsCodeData.bzmc);//包装名称
                $('#packName').val(goodsCodeData.bzmc);//货品名称
                var packId = goodsCodeData.packId;
                var warehouseunit = wareHouseUnit(packId);
                warehouseOption(warehouseunit);
                formSelectApply(data.id);
            }
        })

        function formSelectApply(hpid){

            // 给批次号和生产日期赋option
            var appointBatch = QueryAppointBatch({hpid: hpid});
            var appointBatchOption = '<option value="">不指定</option>';
            var Batchdata = optionRendering(appointBatch, 1, 'hppc', 'hppc');
            if (Batchdata) {
                appointBatchOption += Batchdata;
            }
//                appointBatchOption += optionRendering(appointBatch, 1, 'hppc', 'hppc');
            $('#appointBatch').find("option").remove();
            $('#appointBatch').append(appointBatchOption);

            var appointDate = QueryAppointDate({hpid: hpid});
            var appointDateOption = '<option value="">不指定</option>';
            var Datedata = optionRendering(appointDate, 1, 'scrq', 'scrq');
            if (Datedata) {
                appointDateOption += Datedata;
            }
//                appointDateOption += optionRendering(appointDate, 1, 'scrq', 'scrq');
            $('#appointDate').find("option").remove();
            $('#appointDate').append(appointDateOption);

            //解除批次号和生产日期的禁用
            $('#appointBatch').attr("disabled",false);
            $('#appointDate').attr("disabled",false);
            form.render('select');
        }

        // 给入库单位赋option
        function warehouseOption(data) {
            $('#warehouse').find("option").remove();
            var options = '<option value="">请选择</option>';
            data.forEach(function (item) {
                options += "<option title='"+ JSON.stringify(item) +"' value='"+ item.ms +"'>"+ item.ms +"</option>";
            });
            $('#warehouse').append(options);
            form.render();
            return;
        }

        // 入库单位选中触发
        form.on('select(warehouse)', function (data) {
            var datas = $('#warehouse').find("option:selected");
            console.log('warehousewarehouse', datas);
            var wareHouseData = JSON.parse(datas[0].title);
            $('#volume').val(wareHouseData.tj);
            $('#volumeS').text(wareHouseData.tj);

            $('#suttle').val(wareHouseData.jz);
            $('#suttleS').text(wareHouseData.jz);

            $('#grossWeight').val(wareHouseData.zl);
            $('#grossWeightS').text(wareHouseData.zl);

            $('#money').val(wareHouseData.je);
            $('#numMoney').val(0);
            $('#unitNumber').val(0);
            $('#boxNumber').val('0箱0件');

            $('#warehouseSpan').text(wareHouseData.id);

            packageType(wareHouseData.bzlx);
            // 主单位数量
            UnitNum = wareHouseData.zdwsl;
            UnitType = wareHouseData.bzlx;

        })

        // 监听主单位数量
        $('#unitNumber').on('input propertychange', function(data) {
            var value =  data.currentTarget.value;
            var len, lenfloat;
            var volumeS = parseFloat($('#volumeS').text());
            var suttleS = parseFloat($('#suttleS').text());
            var grossWeightS = parseFloat($('#grossWeightS').text());
            var money = parseFloat($('#money').val());
            // 如果value不为空
            if (value && value !== '0') {
                len = parseInt(value);
                lenfloat = parseFloat(value);
                if (value.indexOf('-') >= 0) {
                    $('#unitNumber').val('');
                } else if ((lenfloat - len) > 0) {
                    $('#unitNumber').val(len);
                } else {
                    $('#volume').val(toFixeds(volumeS * len));
                    $('#suttle').val(toFixeds(suttleS * len));
                    $('#grossWeight').val(toFixeds(grossWeightS * len));
                    $('#numMoney').val(toFixeds(money * len));

                    // 如果主单位数量为1
                    if (UnitNum === 1) {
                        $('#boxNumber').val(len.toString() + '箱0件');
                    } else {
                        var BOX,PIECE;
                        BOX = parseInt(len/UnitNum);
                        PIECE = len%UnitNum;
                        $('#boxNumber').val(BOX + '箱'+ PIECE +'件');
                    }

                }

            } else {
                $('#volume').val(volumeS);
                $('#suttle').val(suttleS);
                $('#grossWeight').val(grossWeightS);
                $('#numMoney').val(0);
                $('#boxNumber').val('0箱0件');
            }

        })

        // 监听箱单位数量
        $('#boxNumber').on('input propertychange', function(data) {
            var value, indbox, indpiece, box, piece, boxs, pieces;
            var volumeS = parseFloat($('#volumeS').text());
            var suttleS = parseFloat($('#suttleS').text());
            var grossWeightS = parseFloat($('#grossWeightS').text());
            var money = parseFloat($('#money').val());
            value =  data.currentTarget.value;
            indbox = value.indexOf('箱');
            indpiece = value.indexOf('件');
            box = value.substring(0, indbox);
            piece = value.substring(indbox + 1,  indpiece);
            boxs = parseFloat(box) - parseInt(box);
            pieces = parseFloat(piece) - parseInt(piece);
            if (boxs > 0 && pieces > 0) {
                $('#unitNumber').val(box + '箱'+ piece +'件');
            }
            if (boxs < 0 && pieces > 0) {
                $('#unitNumber').val(Math.abs(box) + '箱'+ piece +'件');
            }
            if (boxs > 0 && pieces < 0) {
                $('#unitNumber').val(box + '箱'+ Math.abs(piece) +'件');
            }
            if (parseInt(box) >= 0 && parseInt(piece) >= 0) {
                var unitNumber = parseInt(box) * UnitNum + parseInt(piece);
                var len;
                if (unitNumber !== 0) {
                    len = unitNumber;
                    $('#numMoney').val(toFixeds(money * len));
                } else {
                    len = 1;
                    $('#numMoney').val(0);
                }
                $('#unitNumber').val(unitNumber);
                $('#volume').val(toFixeds(volumeS * len));
                $('#suttle').val(toFixeds(suttleS * len));
                $('#grossWeight').val(toFixeds(grossWeightS * len));

            }
        })

        // 行内按钮
        window.operateEvent ={

            // 触发table行内回显
            "click #editRow":function (e, value, row, index) {
                console.log('rrrrrrrr', row);
                $('#tableForm').css('display','block');
                $('#addInfor').css('display','none');
                addType = 'edit';
                formApply();
                var ids1 = {'bh': row.goodsbh};
                var goodsCodeData1 = goodsCode(ids1)[0];
                var packId = goodsCodeData1.packId;
                var warehouseunit = wareHouseUnit(packId);// 查询入库单位
                warehouseOption(warehouseunit);// 给入库单位赋option
                formSelectApply(row.hpid);// 给指定批次和指定生产日期赋option

                var warehouseSel = [];
                // 获取选中的入库单位
                warehouseunit.forEach(function (item) {
                    if (item.ms === row.ms) {
                        warehouseSel.push(item);
                    }
                });
                var warehousesel = warehouseSel[0];
                var formdata = {};
                console.log(66666666666, row.hpid);
//                $('#goodsCode').val([row.hpid]).trigger('change');
                $("#goodsCode").append(new Option(row.goodsbh, row.hpid, false, true));
//                row.goodsCode = row.hpid;
                row.volume = row.tj;// 体积
                row.suttle = row.jz;// 净重
                row.grossWeight = warehousesel.zl * parseInt(row.yqsl);// 毛重
                $('#volumeS').text(warehousesel.tj);
                $('#suttleS').text(warehousesel.jz);
                $('#grossWeightS').text(warehousesel.zl);
                row.money = warehousesel.je;// 单价
                row.numMoney = row.je;// 总金额
                row.unitNumber = parseInt(row.yqsl);// 主单位数量
                row.boxNumber = row.yqxdwsl;// 箱单位数量
                row.appointPlace = row.kwid;//指定库位的库位id
                row.appointDate = row.scrq + '.0';// 指定生产日期
                row.appointBatch = row.pc;//指定批次
                // row中没有备注，于是修改备注无效
                zzpUtils.loadJsonDataToForm(row);
                $('#INDEXS').text(row.index);
                form.render();
                packageType(warehousesel.bzlx);
                UnitNum = warehousesel.zdwsl;

            },
            "click #delete":function (e, value, row, index) {
                console.log('触发了删除', row);
                var SaveData = {}, ids = [];
                ids.push(row.id);
                SaveData.id = ID;
                SaveData.ids = ids;
                $.ajax({
                    url: '/zxsdwms/wmsshipmentorderdetailed/delete',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(SaveData),
                    success: function (data) {
                        layer.msg(data.message);
                        if (data.type === 'success') {
                            refresh();

                        }
                    },
                    error: function (err) {
                        console.log(err);
                    }
                })
            },
            "click #updateZbfh":function(e, value, row, index) {
                var data = {id: row.id};
                console.log('row', row);
                if (row.zbfh === '1') {
                    data.zbfh = '0';
                } else {
                    data.zbfh = '1';
                }
                $.ajax({
                    url: '/zxsdwms/wmsshipmentorder/updateZbfh',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        layer.msg(data.title);
                        if (data.type === 'success') {

                            setTimeout(function () {
                                layer.msg(data.title);
                                window.location.reload()
                            }, 500);
                        //  setTimeout(window.location.reload(),10000);
                        }
                    },
                    error: function(err) {
                        console.log(err);
                    }
                })
            }

        }

        function operateFormatter(value, row, index) {
            var type;
            if(row.zbfh === '1'){
                type = '标记暂不发货'
            } else {
                type = '取消暂不发货'
            }
            return [
                '<a class="layui-btn layui-btn-xs layui-btn-normal" id="updateZbfh" style="text-decoration: none;">' + type + '</a>',
                '<a class="layui-btn layui-btn-xs layui-btn-normal" id="editRow" style="text-decoration: none;">编辑</a>',
                '<a class="layui-btn layui-btn-normal layui-btn-xs" id="delete" style="text-decoration: none;">删除</a>'
            ].join('')
        }

        // 初始化一个空table
        var blanktable = zzpUtils.table({
            elem: '#table',
            isApi: true,
            pagination: false,//分页开关
            checkboxHeader: false,
            showColumns: false,// 列选择
            columns: [
                {field:'goodsbh',title:'货品编号'},
                {field:'goodsName',title:'货品名称'},
                {field:'packName',title:'包装名称'},
                {field:'warehouse',title:'主单位'},
                {field:'unitNumber',title:'预期数量'},
                {field:'boxNumber',title:'预期数量'},
                {field:'fpsl',title:'分配数量'},
                {field:'jhsl',title:'拣货数量'},
                {field:'fysl',title:'发运数量'},
                /*{field:'kwmc',title:'库位名称'},*/
              /*  {field:'pc',title:'批次号'},*/
             /*   {field:'scrq',title:'生产日期'},*/
                {field:'show_fymxzt',title:'SO状态'},
                {field:'show_zbfh',title:'暂不发货'},
                {
                    field:'caozuo',
                    title: '操作',
                    events:operateEvent,
                    formatter:operateFormatter
                }],
        });

        var initTableData = querytable();
        blanktable.bootstrapTable('append', initTableData);

        // 新增按钮触发
        window.tableFormShow = function () {
            console.log('新增触发')
            $('#tableForm').css('display','block');
            $('#addInfor').css('display','none');
            addType = 'add';
            formApply();
        }
        function formApply() {
            $('#tableForm input').val('');//重置input
            $('#tableForm select').val('');
            console.log(11111111111)
            form.render('select');
//            var GoodsCode = goodsCode({});
//
//            var goodsOptionInf = '<option></option>';
//            goodsOptionInf += optionRendering(GoodsCode, 1, 'goodsId', 'goodsBh');
//            // 给货品编号赋option
//            $('#goodsCode').find("option").remove();
//            $('#goodsCode').append(goodsOptionInf);
            $('#goodsCode').select2({
                ajax: {
                    url: '/zxsdwms/wmsshipmentorder/getGoodsByHzid',
                    dataType: 'json',
                    type: 'POST',
                    contentType: "application/json",
                    data: function (params) {
                        var query = {
                            bh: params.term,
                        };
                        return JSON.stringify(query);
                    },
                    processResults: function (data) {
                        var arr= new Array();
                        console.log(arr);
                        if(data){
                            for (var i=0;i<data.length;i++){
                                arr.push({
                                    id: data[i].goodsId,
                                    text: data[i].goodsBh});
                            }
                        }
                        console.log(arr);
                        // Tranforms the top-level key of the response object from 'items' to 'results'
                        return {
                            results: arr
                        };
                    },
                    cache: true
                },
                language: "zh-CN",
                placeholder: '请输入查询字段',
                allowClear: true
            })

            // 查询指定库位
            var appointPlace = QueryAppointPlace({});
            var PlaceOptionInf = '<option value="">不指定</option>';
            PlaceOptionInf += optionRendering(appointPlace, 1, 'id', 'mc');
            //给指定库位赋option
            $('#appointPlace').find("option").remove();
            $('#appointPlace').append(PlaceOptionInf);
            // 重新渲染form，否则新的option不显示
            form.render();
        }

        // 取消按钮触发
        window.tableFormReset = function(){
            $('#tableForm').css('display','none');
            $('#addInfor').css('display','block');
            $('#tableForm input').val('');
        }

        // 最终保存
        form.on("submit(add)",function(data){
            var field = data.field;
            var saveData = {};
            saveData.bh = field.goodsbh;
            saveData.bz = field.datailRemark;
            saveData.bzmc = field.packName;
            saveData.hpid = field.goodsCode;
            saveData.je = field.numMoney;
            saveData.jz = field.suttle;
            saveData.kwid = field.appointPlace;
            saveData.kwmc = $("#appointPlace").find("option:selected").text();
            saveData.mc = field.goodsName;
            saveData.ms = field.warehouse;
            saveData.ssfyddid = ID;
            saveData.pc = field.appointBatch;// 指定批次
            var scrq = field.appointDate;
            scrq = new Date(scrq);
            saveData.scrq = scrq.getTime();
            saveData.tj = field.volume;
            saveData.yqsl = field.unitNumber;
            saveData.yqxdwsl = field.boxNumber;
            saveData.zdw = $('#warehouseSpan').text();
            saveData.zdwsl = UnitNum;
            saveData.zl = field.grossWeight;

            if (addType === 'add') {
                $.ajax({
                    url: '/zxsdwms/wmsshipmentorderdetailed/add',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(saveData),
                    success: function (data) {
                        layer.msg(data.title);
                        if (data.type === 'success') {
                            $('#tableForm').css('display','none');
                            $('#addInfor').css('display','block');
                            refresh();
                        }
                    },
                    error: function (err) {
                        console.log(err);
                    }
                })
            }
            if (addType === 'edit') {
                saveData.id = field.id;
                $.ajax({
                    url: '/zxsdwms/wmsshipmentorderdetailed/edit',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(saveData),
                    success: function (data) {
                        layer.msg(data.title);
                        if (data.type === 'success') {
                            $('#tableForm').css('display','none');
                            $('#addInfor').css('display','block');
                            refresh();
                        }
                    },
                    error: function (err) {
                        console.log(err);
                    }
                })
            }
            return false;
        })

        function refresh() {
            var tabledata1 = querytable();
            blanktable.bootstrapTable('removeAll');
            blanktable.bootstrapTable('append', tabledata1);
        }
    });

</script>
</html>